﻿using DAL;
using Mode;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    /// <summary>
    /// 业务逻辑操作
    /// </summary>
    public class BusinessLogic
    {
        private BookInfoDAL book;
        private UserInfoDAL user;//定义用户数据访问类对象user
        public BusinessLogic ()
        {
            book = new BookInfoDAL() ;
            user = new UserInfoDAL();//实例化UserInfo用户数据访问类 
        }
        public bool UserLogin(string userid, string password)
        {
            return user.UserLogin(userid, password);
        }
        public DataSet GetBookClass()
        {
            return book.GetBookClass();
        }
        public bool CheckingDulication(Bookinfo bo)
        {
            return book.CheckingDulication(bo);
        }
        public Bookclass  FindBookClass(string bookClassID = null, string bookClassName = null)
        {
            return book.FindBookClass(bookClassID, bookClassName);
        }
        public object EditBook(Bookinfo bo,string operation)
        {
            if (operation=="I"&&CheckingDulication(bo)==true )
            {
                return "这是重复记录";
            }
            else
            {
                book.EditBook(bo, operation);
                return book;
            }
        }
        public bool GetUserInfoByUserid(userinfo  bo)
        {
            return book.GetUserInfoByUserid(bo);
        }
        public object EditClass(userinfo  bo,string operation)
        {
            if (operation =="I"&& GetUserInfoByUserid(bo )==true )
            {
                return "这是重复记录";
            }
            else
            {
                book.EditClass(bo, operation);
                return book;
            }
        }
    
        /// <summary>
        /// 判断数据是否为非空
        /// </summary>
        /// <param name="ds">d1</param>
        /// <returns></returns>
        public bool Judge(DataSet ds)
        {
            if (ds.Tables.Count == 1 && ds.Tables[0].Rows.Count == 0)
            {
                return false;
            }
            else
                return true;
        }
        public DataSet GetBookByBookNameOrClassname(string bookname,string classname)
        {
            DataSet ds = book.GetBookByBookNameOrClassname(bookname, classname);
            if (Judge(ds))
                return book.GetBookByBookNameOrClassname(bookname, classname);
            else
                return null;
        }
        public DataSet GetUserByUserIdOrClassname(string UserId, string classname)
        {
            DataSet ds = book.GetUserByUserIdOrClassname(UserId, classname);
            if (Judge(ds))
                return book.GetUserByUserIdOrClassname(UserId, classname);
            else
                return null;
        }
        public DataSet GetBookByBookId(string bookid)
        {
             return book.GetBookByBookId(bookid);
        }
        public bool DeleteBook(string bookid)
        {
            return book.DeleteBook(bookid);
        }

        /// <summary>
        /// 图书借阅模块
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="userName"></param>
        /// <returns></returns>
        public DataSet GetUserInfo(string userid=null ,string userName=null )
        {
            return user.GetUserInfo(userid, userName);
        }
        public MemoryStream GetPhotoByUserID(string userid)
        {
            return user.GetPhotoByUserID(userid);
        }
        public DataSet GetBorrowBookByUserID(string userid)
        {
            return book.GetBorrowBookByUserID(userid);
        }
        public object InsertBorrowInfo(string bookid, string userid)
        {
            if (book.BookSateByBookID(bookid) )
                return book.InsertBorrowInfo(bookid, userid);
            else
                return "不可借";
        }
        public DataSet GetBorrowInfo(string userid ,string bookid)
        {
            
                return book.GetBorrowInfo(userid, bookid);
            
        }
        public bool ReturnBook(string bookid)
        {
            return book.ReturnBook(bookid);
        }
        public bool RenewBook(string bookid)
        {
            return book.RenewBook(bookid);
        }
        public DataSet GetClassInfo()
        {
            return book.GetClassInfo();
        }
        public Classinfo FindClassInfo(int ClassName = 0, string ClassID = null)
        {
            return book.FindClassInfo( ClassName,ClassID);
        }
    }
}
